
from xmindparser import xmind_to_dict
import os, xlwt
from tkinter.messagebox import showinfo
import tkinter.messagebox


class xmind_to_csv():

    def __init__(self):
        self.workbook = xlwt.Workbook(encoding='utf-8')
        self.worksheet = self.workbook.add_sheet('sheet1', cell_overwrite_ok=True)

    def numberLen(self, value, errornum=None):
        try:
            return len(value['topics'])
        except KeyError:
            if errornum == 2:
                tkinter.messagebox.askokcancel('提示', '用例有前置条件 "{0}",没有测试步骤和预期结果喔！ 请确认是否如此！'.format(value['title']))
                print('案例 "{0}",没有测试步骤和预期结果喔！ 请确认是否如此！'.format(value['title']))
            if errornum == 3:
                tkinter.messagebox.showinfo(title='提示',
                                            message='用例有前置条件 "{0}",没有预期结果喔！ 请填写后重新执行！'.format(value['title']))
                print('案例 "{0}",没有预期结果喔！ 请填写后重新执行！'.format(value['title']))
            return 0

    def xmind_title(self, value):
        """获取xmind标题内容"""
        return value['title']

    def writeExcel(self, row, case, excelName):
        sort = 0
        row0 = ["用例目录", '用例名称', '前置条件', '用例步骤', '预期结果', "用例类型", '用例状态', '用例等级', '创建人']
        # 生成第一行
        for i in range(0, len(row0)):
            self.worksheet.write(0, i, row0[i])

        for key, value in case.items():
            self.worksheet.write(row, sort, value)
            sort = sort + 1
        self.workbook.save(excelName + '.xls')

    def readXmind(self, FileName, operator='ditto'):

        self.rowNum = 0  # 计算测试用例的条数
        self.caseDict = {}
        self.XmindContent = xmind_to_dict(FileName)[0]['topic']  # xmind内容
        print(self.XmindContent)
        self.XmindTitle = self.xmind_title(self.XmindContent)
        print(self.XmindTitle)
        TestSuitMunFlag = self.numberLen(self.XmindContent, 0)
        for TestSuitMun in range(TestSuitMunFlag):
            TestCaseMunFlag = self.numberLen(self.XmindContent['topics'][TestSuitMun], 1)
            print('TestCaseMunFlag+' + str(TestCaseMunFlag))
            for TestCaseMun in range(TestCaseMunFlag):
                TestStepMunFlag = self.numberLen(self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun], 2)
                print('TestStepMunFlag+' + str(TestStepMunFlag))
                # 执行步骤跟预期结果为空的时候
                if TestStepMunFlag == 0:
                    self.caseDict['myTestCase'] = self.XmindContent['title']
                    self.caseDict['TestCase'] = self.XmindContent['topics'][TestSuitMun]['title']
                    self.caseDict['Testprecondition'] = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun][
                        'title']
                    self.caseDict['TestStep'] = ''
                    self.caseDict['TestResult'] = ''
                    self.caseDict['Testbelong'] = ''
                    self.caseDict[
                        'Teststatus'] = '正常'  # self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][0]['makers'][0]
                    Testpriority1 = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['makers'][0]
                    if Testpriority1 == 'priority-1':
                        Testpriority = '高'
                    elif Testpriority1 == 'priority-2':
                        Testpriority = '中'
                    else:
                        Testpriority = '低'
                    self.caseDict['Testpriority'] = Testpriority
                    self.caseDict['Testoperator'] = operator
                    # print(self.caseDict['Testpriority'])

                    # self.caseDict['myTestCase'] = self.caseDict['TestSuit'] + '/' + self.caseDict['TestCase']
                    # print(self.rowNum,self.caseDict['TestSuit'] + '/' + self.caseDict['TestCase'])
                    self.rowNum = self.rowNum + 1
                    print("*************")
                    self.writeExcel(self.rowNum, self.caseDict, self.XmindTitle)

                for TestStepMun in range(TestStepMunFlag):
                    TestResultFlag = self.numberLen(
                        self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun], 3)
                    # TestResultFlag1 = self.numberLen(self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][TestStepMun],4)
                    # print('TestResultFlag1+'+str(TestResultFlag1))
                    # 预期结果不能为空
                    if TestResultFlag != 0:
                        self.caseDict['myTestCase'] = self.XmindContent['title']
                        self.caseDict['TestCase'] = self.XmindContent['topics'][TestSuitMun]['title']
                        self.caseDict['Testprecondition'] = \
                        self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['title']
                        self.caseDict['TestStep'] = \
                        self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['title']
                        self.caseDict['TestResult'] = \
                        self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun][
                            'topics'][0]['title']
                        # self.caseDict['TestResult'] = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][0]['title']
                        # print(self.caseDict['TestResult'])
                        self.caseDict['Testbelong'] = '功能测试'
                        self.caseDict[
                            'Teststatus'] = '正常'  # self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][0]['makers'][0]
                        Testpriority1 = self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['makers'][0]
                        if Testpriority1 == 'priority-1':
                            Testpriority = '高'
                        elif Testpriority1 == 'priority-2':
                            Testpriority = '中'
                        else:
                            Testpriority = '低'
                        self.caseDict['Testpriority'] = Testpriority

                        # self.caseDict['Testpriority']=self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['makers'][0]

                        self.caseDict['Testoperator'] = operator
                        # self.caseDict['myTestCase']= self.XmindContent['topics'][TestSuitMun]['topics'][TestCaseMun]['topics'][TestStepMun]['topics'][TestStepMun]['topics'][0]['title']
                        # self.caseDict['TestSuit']+'/'+self.caseDict['TestCase']+'-'+self.caseDict['TestStep']+'-'+self.caseDict['TestResult']
                        # print(self.rowNum,len(self.caseDict['TestSuit']+'/'+self.caseDict['TestCase']+'-'+self.caseDict['TestStep']+'-'+self.caseDict['TestResult']),self.caseDict['TestSuit']+'/'+self.caseDict['TestCase']+'-'+self.caseDict['TestStep']+'-'+self.caseDict['TestResult'])
                        self.rowNum = self.rowNum + 1
                        self.writeExcel(self.rowNum, self.caseDict, self.XmindTitle)

        showinfo(title='转换结束', message='生成{0}条用例，请检查是否有误。'.format(self.rowNum))


if __name__ == '__main__':
    XmindFile = os.path.join(os.path.dirname(__file__), 'xmind_excel.xmind')  # xmind文件
    operator = 'dididdi'
    xmind_to_csv().readXmind('C:\\Users\\Administrator\\Documents\\2测试案例点归档\\测试用例规范.xmind', operator)